{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 说明\n",
    "\n",
    "当在编译一个设备驱动模块时，需要在系统中安装内核头文件。内核头文件同样在编译与内核直接链接该的用户空间程序时需要。当在这些情况下安装内核头文件时，必须确保内核头文件精确地与当前内核版本匹配（比如：3.13.0-24-generic）。\n",
    "\n",
    "- 如果内核是发行版自带的内核版本，或者使用默认的包管理器的基础仓库升级的（比如：apt-get、yum），也可以使用包管理器来安装内核头文件。\n",
    "- 如果下载的是kernel源码并且手动编译的，可以使用make命令来安装匹配的内核头文件。\n",
    "\n",
    "## 查看内核版本\n",
    "\n",
    "```sh\n",
    "uname -r\n",
    "```\n",
    "\n",
    "## 安装\n",
    "\n",
    "###  使用apt-get命令来安装匹配的内核文件\n",
    "\n",
    "1. 首先，使用dpkg-query命令检查是否有可用的内核头文件\n",
    "\n",
    "```sh\n",
    "$ dpkg-query -s linux-headers-$(uname -r)\n",
    "dpkg-query: package 'linux-headers-3.11.0-26-generic' is not installed and no information is available\n",
    "```\n",
    "\n",
    "2. 接着使用下面的命令安装匹配的内核文件\n",
    "\n",
    "```sh\n",
    "# 头文件\n",
    "sudo apt-get install linux-headers-$(uname -r)\n",
    "# 镜像文件\n",
    "sudo apt-get install linux-image-$(uname -r)\n",
    "```\n",
    "\n",
    "3. 验证文件是否成功安装\n",
    "\n",
    "```sh\n",
    "$ dpkg-query -s linux-headers-$(uname -r)\n",
    "Package: linux-headers-3.11.0-26-genericStatus: install ok installed\n",
    "```\n",
    "\n",
    "4、Debian、Ubuntu、Linux Mint默认头文件在/usr/src下\n",
    "\n",
    "### 使用yum命令来安装匹配的内核头文件\n",
    "\n",
    "1. 首先，用下面的命令检查系统是否已经安装了头文件。如果下面的命令没有任何输出，这就意味着还没有头文件\n",
    "\n",
    "```sh\n",
    "$ rpm -qa | grep kernel-headers-$(uname -r)\n",
    "```\n",
    "\n",
    "2. 接着用yum命令安装头文件。这个命令会自动找出合适的头文件并安装\n",
    "\n",
    "```sh\n",
    "$ sudo yum install kernel-headers\n",
    "```\n",
    "\n",
    "3. 验证包安装的状态\n",
    "\n",
    "```sh\n",
    "$ rpm -qa | grep kernel-headers-$(uname -r)\n",
    "kernel-headers-3.10.0-123.9.3.el7.x86_64\n",
    "```\n",
    "\n",
    "4. Fedora、CentOS 或者 RHEL上默认内核头文件的位置是/usr/include/linux。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 卸载相关内核文件\n",
    "\n",
    "Debian、Ubuntu、Linux Mint默认文件在/usr/src下，可先查看具体要卸载的文件名。\n",
    "\n",
    "```sh\n",
    "sudo apt-get purge linux-image-xxx\n",
    "sudo apt-get purge linux-headers-xxx\n",
    "\n",
    "sudo update-initramfs -u\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "210.824px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
